const MinminToken = artifacts.require('MinminToken.sol')
const Exchange = artifacts.require('Exchange.sol')

module.exports = async function(callback) {
  const minminToken = await MinminToken.deployed()
  const exchange = await Exchange.deployed()
  const accounts = await web3.eth.getAccounts()
  const ETHER_ADDRESS = '0x0000000000000000000000000000000000000000'

  // ...

  console.log('----start---');
  // 转入交易所 100 以太币
  await exchange.depositEther({
    form: accounts[0],
    value: toWei(100)
  })

  const res = await exchange.tokens(ETHER_ADDRESS, accounts[0])
  console.log('Exchaneg-ETHER:' + fromWei(res));

  // 转出 50以太币
  await exchange.withdrawEther(toWei(50), {
    from: accounts[0]
  })
  const res1 = await exchange.tokens(ETHER_ADDRESS, accounts[0])
  console.log('Exchange-Ether:' + fromWei(res1))

  const res2 = await minminToken.balanceOf(accounts[0])
  console.log('min:' + fromWei(res2));

  await minminToken.approve(exchange.address, toWei(1000), {
    from: accounts[0]
  })

  await exchange.depositToken(minminToken.address, toWei(1000), {
    from: accounts[0]
  })

  const res3 = await minminToken.balanceOf(accounts[0])
  console.log('min:' + fromWei(res3));

  const res4 = await exchange.tokens(minminToken.address, accounts[0])
  console.log('ex-min:' + fromWei(res4));

  await exchange.withdrawToken(minminToken.address, toWei(50), {
    from: accounts[0]
  })

  const res5 = await exchange.tokens(minminToken.address, accounts[0])
  console.log('ex-min:' + fromWei(res5));

  const res6 = await minminToken.balanceOf(accounts[0])
  console.log('min:' + fromWei(res6));

  callback()
}

function fromWei(val) {
  return web3.utils.fromWei(val, 'ether')
}

function toWei(val) {
  return web3.utils.toWei(val.toString(), 'ether')
}